package com.jstyle.jclife.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.model.NetResultData;

import com.jstyle.jclife.utils.NetWorkConast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ManageLoginActivity extends BaseActivity {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_forgotpwd_title)
    TextView tvForgotpwdTitle;
    @BindView(R.id.tv_manage_tips)
    TextView tvManageTips;
    @BindView(R.id.bt_update_email)
    Button btUpdateEmail;
    @BindView(R.id.view_divider)
    View viewDivider;
    @BindView(R.id.bt_update_pwd)
    Button btUpdatePwd;
    @BindView(R.id.tv_email_alert)
    TextView tvEmailAlert;
    @BindView(R.id.tv_resend_email)
    TextView tvResendEmail;
    private Disposable activateDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mananger_login);
        ButterKnife.bind(this);
        init();
    }

    @OnClick({R.id.iv_back, R.id.bt_update_email, R.id.bt_update_pwd, R.id.tv_resend_email})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_update_email:
                startActivity(UpdateEmailActivity.class);
                break;
            case R.id.bt_update_pwd:
                startActivity(UpdatePwdActivity.class);
                break;
            case R.id.tv_resend_email:
                resendEmail();
                break;
        }
    }

    private void resendEmail() {
        String uid= NetWorkUtil.getUserId();



        NetWorkUtil.getInstance().getJstyleApi().activateEmail(uid).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                activateDisposable =disposable;
                showProgressDialog(getString(R.string.wait));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData netResultData) {
                disMissProgressDialog();
                int msgCode = netResultData.getMsgCode();
                String msg = NetWorkConast.getResult(msgCode, ManageLoginActivity.this);
                if (msgCode == NetResultData.Success_Code) {
                    showToast(getString(R.string.resend_email_success));
                    //   msg = getString(R.string.login_complete);
                    // sendEmailSuccessful();
                }else{
                    showToast(msg);
                }
                //
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void init() {
        tvResendEmail.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(activateDisposable);
    }

    @Override
    protected void ActivateFinished() {
        super.ActivateFinished();
        tvEmailAlert.setVisibility(View.GONE);
        tvResendEmail.setVisibility(View.GONE);
    }

    @Override
    protected void ActivateUnFinished() {
        super.ActivateUnFinished();
        tvEmailAlert.setVisibility(View.VISIBLE);
        tvResendEmail.setVisibility(View.VISIBLE);
    }
}
